package com.yang.dao;

import com.yang.entity.Order;

import java.sql.*;
import java.util.*;

public class OrderDao {
    //查询全部
    public List<Order> queryOrder() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Order");
            rs = ps.executeQuery();
            List<Order> list = new ArrayList<>();
            while (rs.next()) {
                Order obj = new Order();
                obj.setId(rs.getString("id"));
                obj.setUserName(rs.getString("userName"));
                obj.setAddress(rs.getString("address"));
                obj.setCreationTime(rs.getDate("creationTime"));
                obj.setTel(rs.getString("tel"));
                obj.setTotalAmount(rs.getObject("totalAmount"));
                obj.setStatus(rs.getInt("status"));
                obj.setUserId(rs.getInt("userId"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Order queryOrderById(String id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Order where id=?");
            ps.setString (1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Order obj = new Order();
                obj.setId(rs.getString("id"));
                obj.setUserName(rs.getString("userName"));
                obj.setAddress(rs.getString("address"));
                obj.setCreationTime(rs.getDate("creationTime"));
                obj.setTel(rs.getString("tel"));
                obj.setTotalAmount(rs.getObject("totalAmount"));
                obj.setStatus(rs.getInt("status"));
                obj.setUserId(rs.getInt("userId"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveOrder(Order obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into order(user_name, address, creation_time, tel, total_amount, status, user_id) values (?, ?, ?, ?, ?, ?, ?)");
            ps.setString(1, obj.getUserName());
            ps.setString(2, obj.getAddress());
            ps.setDate(3, obj.getCreationTime());
            ps.setString(4, obj.getTel());
            ps.setObject(5, obj.getTotalAmount());
            ps.setInt(6, obj.getStatus());
            ps.setInt(7, obj.getUserId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteOrderById(String id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Order where id=?");
            ps.setString (1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateOrderById(Order obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update order set user_name=?, address=?, creation_time=?, tel=?, total_amount=?, status=?, user_id=? where id=?");
            ps.setString(1, obj.getUserName());
            ps.setString(2, obj.getAddress());
            ps.setDate(3, obj.getCreationTime());
            ps.setString(4, obj.getTel());
            ps.setObject(5, obj.getTotalAmount());
            ps.setInt(6, obj.getStatus());
            ps.setInt(7, obj.getUserId());
            ps.setString(8, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
